/**
 * 
 */
package com.py.framework.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.py.framework.common.controller.SystemController;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.entity.SysElement;
import com.py.framework.service.SysElementService;

/**
 * 
 * 系统定制化Controller
 * 
 * @author dd
 *
 */
@RestController
@RequestMapping("/sysElement")
public class SysElementController extends SystemController {

	@Autowired
	private SysElementService sysElementService;

	@RequestMapping("/add.json")
	public RestServiceResult<Object> add(
			@RequestParam(value = "loginLogo", required = false) CommonsMultipartFile loginLogo,
			@RequestParam(value = "favicon", required = false) CommonsMultipartFile favicon,
			@RequestParam(value = "background", required = false) CommonsMultipartFile background,
			@RequestParam(value = "loginTitle", required = false) CommonsMultipartFile loginTitle,
			@RequestParam(value = "copyright", required = false) String copyright,
			@RequestParam(value = "productName", required = false) String productName) throws IOException {

		RestServiceResult<Object> result = super.getSuccessRestServiceResult();

		sysElementService.addSysElement(favicon, background, loginLogo, null, null, loginTitle, copyright, productName,
				null);

		return result;

	}

	@RequestMapping("/addBackEndElement.json")
	public RestServiceResult<Object> addBackEndElement(
			@RequestParam(value = "sysLogo", required = false) CommonsMultipartFile sysLogo,
			@RequestParam(value = "scalingSysLogo", required = false) CommonsMultipartFile scalingSysLogo,
			@RequestParam(value = "webUrl", required = false) String webUrl) throws IOException {

		RestServiceResult<Object> result = super.getSuccessRestServiceResult();

		sysElementService.addSysElement(null, null, null, sysLogo, scalingSysLogo, null, null, null, webUrl);

		return result;

	}

	@RequestMapping("/getLoginLogo.png")
	public void getLoginLogo(HttpServletResponse response) throws IOException {
		SysElement record = sysElementService.getLatestRecord();
		response.setContentType("image/png");
		response.setCharacterEncoding("UTF-8");
		OutputStream outputSream = response.getOutputStream();
		InputStream in = new ByteArrayInputStream(record.getLoginLogo());
		int len = 0;
		byte[] buf = new byte[1024];
		while ((len = in.read(buf, 0, 1024)) != -1) {
			outputSream.write(buf, 0, len);
		}
		outputSream.close();

	}

	@RequestMapping("/getFavicon.ico")
	public void getFavicon(HttpServletResponse response) throws IOException {
		SysElement record = sysElementService.getLatestRecord();
		response.setContentType("image/png");
		response.setCharacterEncoding("UTF-8");
		OutputStream outputSream = response.getOutputStream();
		InputStream in = new ByteArrayInputStream(record.getFavicon());
		int len = 0;
		byte[] buf = new byte[1024];
		while ((len = in.read(buf, 0, 1024)) != -1) {
			outputSream.write(buf, 0, len);
		}
		outputSream.close();

	}

	@RequestMapping("/getWebUrl.json")
	public RestServiceResult<String> getWebUrl(HttpServletResponse response) throws IOException {

		SysElement record = sysElementService.getLatestRecord();

		RestServiceResult<String> successRestServiceResult = super.getSuccessRestServiceResult();

		if (record != null) {

			successRestServiceResult.setData(record.getWebUrl());
		}

		return successRestServiceResult;

	}

	@RequestMapping("/getSysLogo.png")
	public void getSysLogo(HttpServletResponse response) throws IOException {
		SysElement record = sysElementService.getLatestRecord();

		if (record != null && record.getSysLogo() != null) {
			response.setContentType("image/png");
			response.setCharacterEncoding("UTF-8");
			OutputStream outputSream = response.getOutputStream();
			InputStream in = new ByteArrayInputStream(record.getSysLogo());
			int len = 0;
			byte[] buf = new byte[1024];
			while ((len = in.read(buf, 0, 1024)) != -1) {
				outputSream.write(buf, 0, len);
			}
			outputSream.close();
		}

	}

	@RequestMapping("/getBackground.png")
	public void getBackground(HttpServletResponse response) throws IOException {
		SysElement record = sysElementService.getLatestRecord();

		if (record != null && record.getBackground() != null) {
			response.setContentType("image/png");
			response.setCharacterEncoding("UTF-8");
			OutputStream outputSream = response.getOutputStream();
			InputStream in = new ByteArrayInputStream(record.getBackground());
			int len = 0;
			byte[] buf = new byte[1024];
			while ((len = in.read(buf, 0, 1024)) != -1) {
				outputSream.write(buf, 0, len);
			}
			outputSream.close();
		}

	}

	@RequestMapping("/getScalingSysLogo.png")
	public void getScalingSysLogo(HttpServletResponse response) throws IOException {
		SysElement record = sysElementService.getLatestRecord();

		if (record != null && record.getScalingSysLogo() != null) {
			response.setContentType("image/png");
			response.setCharacterEncoding("UTF-8");
			OutputStream outputSream = response.getOutputStream();
			InputStream in = new ByteArrayInputStream(record.getScalingSysLogo());
			int len = 0;
			byte[] buf = new byte[1024];
			while ((len = in.read(buf, 0, 1024)) != -1) {
				outputSream.write(buf, 0, len);
			}
			outputSream.close();
		}

	}

	@RequestMapping("/getLogoTitle.png")
	public void getLogoTitle(HttpServletResponse response) throws IOException {
		SysElement record = sysElementService.getLatestRecord();

		if (record != null && record.getLoginTitle() != null) {
			response.setContentType("image/png");
			response.setCharacterEncoding("UTF-8");
			OutputStream outputSream = response.getOutputStream();
			InputStream in = new ByteArrayInputStream(record.getLoginTitle());
			int len = 0;
			byte[] buf = new byte[1024];
			while ((len = in.read(buf, 0, 1024)) != -1) {
				outputSream.write(buf, 0, len);
			}
			outputSream.close();
		}
	}

	@RequestMapping("/getProductName.json")
	public RestServiceResult<String> getProductName() {
		SysElement record = sysElementService.getLatestRecord();

		RestServiceResult<String> successRestServiceResult = super.getSuccessRestServiceResult();

		if (record != null) {

			successRestServiceResult.setData(record.getProductName());
		}

		return successRestServiceResult;

	}

	@RequestMapping("/getCopyright.json")
	public RestServiceResult<String> getCopyright() {
		SysElement record = sysElementService.getLatestRecord();

		RestServiceResult<String> successRestServiceResult = super.getSuccessRestServiceResult();

		if (record != null) {

			successRestServiceResult.setData(record.getCopyright());
		}

		return successRestServiceResult;

	}

}
